{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据爬取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "import os\n",
    "import requests\n",
    "import pickle\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "product_url = r'https://item.jd.com/5561746.html#none'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = webdriver.Chrome()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(product_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_comments_url = r'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=5561746&score=3&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'\n",
    "bad_comments_url = r'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=5561746&score=1&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [],
   "source": [
    "header = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',\n",
    "}\n",
    "param = {\n",
    "    'callback': 'fetchJSON_comment98',\n",
    "    'productId': '5561746',\n",
    "    'score': '3',\n",
    "    'sortType': '5',\n",
    "    'page': '',\n",
    "    'pageSize': '10',\n",
    "    'isShadowSku': '0',\n",
    "    'fold': '1',\n",
    "}\n",
    "cookie = {\n",
    "    'Cookie': 'shshshfpa=cb4386b6-c0f2-68a1-b156-2236f499ee30-1590065631; shshshfpb=dXHF9pqH0l8XV8dgbxTlNEQ%3D%3D; __jdu=15900656294791955369661; user-key=3e85b9e4-c7cf-43fd-9e1a-756e9776cab0; cn=0; __jdc=122270672; areaId=19; ipLoc-djd=19-1607-3155-0; shshshfp=83f76a7577a1d3cdcb7f20cd9a99ba87; __jdv=122270672|github.com|-|referral|-|1617588163257; jwotest_product=99; __jda=122270672.15900656294791955369661.1590065629.1617593724.1617596142.26; shshshsID=273af6177249142f26677cc915a91991_2_1617597325824; __jdb=122270672.2.15900656294791955369661|26.1617596142; JSESSIONID=EDBD2A6AD8E18761AFF846595478748F.s1; 3AB9D23F7A4B3C9B=NYA7Y2IYQW7V35YN3PSDHABICJZ5GIKPEEIE6XO7TSEUVYNHVZ7CFQHTY2RYGTGNNEFG2YNVNV5ZYJC36L2IOMHRSM'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "session = requests.Session()\n",
    "cookie = requests.utils.cookiejar_from_dict(cookie)\n",
    "session.cookies = cookie\n",
    "res = session.get(url=good_comments_url, headers=header, data=param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_comments = re.findall(r'fetchJSON_comment98\\((.*)\\)', res.text)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(good_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_comments = json.loads(good_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(good_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_good_comments(header, param, cookie, scrap_page=200):\n",
    "    session = requests.Session()\n",
    "    session.cookies = requests.utils.cookiejar_from_dict(cookie)\n",
    "    scrap_page = scrap_page\n",
    "    fail = 0\n",
    "    comments = []\n",
    "    for page in tqdm(range(scrap_page)):\n",
    "        good_comments_url = f'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=5561746&score=3&sortType=5&page={page}&pageSize=10&isShadowSku=0&fold=1'\n",
    "        try:\n",
    "            res = session.get(url=good_comments_url, headers=header, data=param)\n",
    "            good_comments = re.findall(r'fetchJSON_comment98\\((.*)\\)', res.text)[0] # 本来获取的是json数据，但是前面加了字符串，所以要删除\n",
    "            good_comments = json.loads(good_comments) # 将字符串转换成字典\n",
    "            for itm in good_comments['comments']:\n",
    "                comments.append(itm['content'])\n",
    "            time.sleep(1)\n",
    "        except Exception as e:\n",
    "            fail += 1\n",
    "            continue\n",
    "    print(f'\\n成功的页数为:{scrap_page - fail}失败的页数为:{fail}')\n",
    "    return comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 300/300 [02:59<00:00,  1.67it/s]\n",
      "成功的页数为:119失败的页数为:181\n",
      "\n"
     ]
    }
   ],
   "source": [
    "good_comments = get_good_comments(header=header, param=param, cookie=cookie, scrap_page=150)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(good_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./data/good_comments.pkl', 'wb') as f:\n",
    "    pickle.dump(good_comments, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = pickle.load(open('./data/good_comments.pkl', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_good_comments(header, param, cookie, scrap_page=200):\n",
    "    session = requests.Session()\n",
    "    session.cookies = requests.utils.cookiejar_from_dict(cookie)\n",
    "    scrap_page = scrap_page\n",
    "    fail = 0\n",
    "    comments = []\n",
    "    for page in tqdm(range(scrap_page)):\n",
    "        good_comments_url = f'https://club.jd.com/comment/productPageComments.action'\n",
    "        try:\n",
    "            res = session.get(url=good_comments_url, headers=header, data=param)\n",
    "            good_comments = re.findall(r'fetchJSON_comment98\\((.*)\\)', res.text)[0] # 本来获取的是json数据，但是前面加了字符串，所以要删除\n",
    "            good_comments = json.loads(good_comments) # 将字符串转换成字典\n",
    "            for itm in good_comments['comments']:\n",
    "                comments.append(itm['content'])\n",
    "            time.sleep(1)\n",
    "        except Exception as e:\n",
    "            fail += 1\n",
    "            continue\n",
    "    print(f'\\n成功的页数为:{scrap_page - fail}失败的页数为:{fail}')\n",
    "    return comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pickle\n",
    "import re\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['pool_comments.pkl', 'good_comments.pkl']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir('./data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_comments = pickle.load(open('./data/good_comments.pkl', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pool_comments = pickle.load(open('./data/pool_comments.pkl', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 994)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(set(good_comments)), len(set(pool_comments)) # 怕有些数据重复，所以使用set去重"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 中文文本分词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 数据清洗\n",
    "- 分词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import jieba.posseg\n",
    "import re\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clear_sentence(comments):\n",
    "    for idx, sentence in enumerate(tqdm(comments)):\n",
    "        temp1 = re.sub(\"[a-zA-Z0-9]\", \"\", sentence) # 清除\n",
    "        temp2 = re.sub(\"[\\s+\\.\\!\\/_,$%^*(+\\\"\\'；：“”．]+|[+——！，。？?、~@#￥%……&*（）]+\", \"\", temp1) # 清除标点符号\n",
    "        comments[idx] = temp2\n",
    "    return comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1000/1000 [00:00<00:00, 49969.07it/s]\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 230039.16it/s]\n"
     ]
    }
   ],
   "source": [
    "clr_good_comments = clear_sentence(good_comments)\n",
    "clr_pool_comments = clear_sentence(pool_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'期待已久 的 洗面奶 终于 到手 了 洗面奶 真的 是 好 用 物美 超值 用 完 不 油腻 不 紧绷 清爽 一整天 温和 不 刺激 性价比 也 真的 是 超高 适合 每 一个 年龄段 使用 尤其 是 控油 效果 特别 好 第二次 买 欧莱雅 的 产品 了 品质 非常 不错 以前 都 是 听 朋友 推荐 来 买 的 第一次 用 就 觉得 很 不错 已 推荐 同事 买 了 真心 不错'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "' '.join(jieba.cut(clr_good_comments[0])) #  这里直接返回的是一个迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def seg_comments(comments):\n",
    "    for idx, sentence in enumerate(tqdm(comments)):\n",
    "        comments[idx] = ' '.join(jieba.cut(sentence, cut_all=False))\n",
    "    return comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1000/1000 [00:00<00:00, 2046.45it/s]\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 11547.78it/s]\n"
     ]
    }
   ],
   "source": [
    "seg_good_comments = seg_comments(clr_good_comments)\n",
    "seg_pool_comments = seg_comments(clr_pool_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'期待已久 的 洗面奶 终于 到手 了 洗面奶 真的 是 好 用 物美 超值 用 完 不 油腻 不 紧绷 清爽 一整天 温和 不 刺激 性价比 也 真的 是 超高 适合 每 一个 年龄段 使用 尤其 是 控油 效果 特别 好 第二次 买 欧莱雅 的 产品 了 品质 非常 不错 以前 都 是 听 朋友 推荐 来 买 的 第一次 用 就 觉得 很 不错 已 推荐 同事 买 了 真心 不错'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seg_good_comments[0] # 清洗完后的数据如下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 停用词处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['pool_comments.pkl', 'good_comments.pkl', 'stopWord.txt']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir('./data/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./data/stopWord.txt', 'r') as f:\n",
    "    stop_words = []\n",
    "    for word in f.readlines():\n",
    "        stop_words.append(word.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['：', '；', '？', '人民', '末##末', '啊', '阿', '哎', '哎呀', '哎哟']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stop_words[10:20] # 查看停用词处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['期待已久', '的', '洗面奶', '终于']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seg_good_comments[0].split(' ')[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = []\n",
    "for sentence in seg_good_comments:\n",
    "    temp = ''\n",
    "    for word in sentence.split(' '):\n",
    "        if word not in stop_words:\n",
    "            temp = ' '.join([temp, word])\n",
    "    res.append(temp.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "def del_stop_words(comments):\n",
    "    res = []\n",
    "    for sentence in tqdm(comments):\n",
    "        temp = []\n",
    "        for word in sentence.split(' '):\n",
    "            if word not in stop_words:\n",
    "                temp.append(word.strip())\n",
    "        res.append(temp)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1000/1000 [00:01<00:00, 987.08it/s]\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 6273.73it/s]\n"
     ]
    }
   ],
   "source": [
    "pos_comments = del_stop_words(seg_good_comments)\n",
    "neg_comments = del_stop_words(seg_pool_comments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获得词向量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里获得中文的word2vect向量\n",
    "\n",
    "参考：https://blog.csdn.net/shuihupo/article/details/85162237"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "两个问题：\n",
    "- 句子长度不一样怎么处理\n",
    "- 是使用所有语句进行训练还是分开训练w2v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.models import Word2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "import multiprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "comments = pos_comments + neg_comments # 这里使用全部的语句进行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub_words = [word for sentence in comments for word in sentence] # 进行拆分,相当于下面代码\n",
    "\n",
    "# for sentence in comments:\n",
    "#     for word in sentence:\n",
    "#         sub_words.append(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "count_words = Counter(sub_words) # 字典结构，键为单词，值为出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "sort_words = sorted(count_words.items(), key=lambda x:x[1], reverse=True) # 形成字典结构。但是为按词频从大到小排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [],
   "source": [
    "words = [item[0] for item in sort_words] # 获得排序好的单词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Word2Vec(comments, min_count=1, iter=20, size=128, window=5, workers=multiprocessing.cpu_count()) # 注意输入的格式为[['',''], ['', '']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('./data/w2vModel')\n",
    "# model = Word2VecModel.load('./data/w2vModel')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-102-0a8c86aa1953>:1: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  len(comments[2]), model[comments[2]].shape\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(29, (29, 128))"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(comments[2]), model[comments[2]].shape # 句子长度不一样，怎么处理？将每个词转换成了向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-106-972a839c011c>:1: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  len(model[comments[2]]), np.sum(model[comments[2]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(29, -24.246601)"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(model[comments[2]]), np.sum(model[comments[2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_embedding(words, model):\n",
    "    '''\n",
    "    构建look up table\n",
    "    '''\n",
    "    vocab = []\n",
    "    embedding = []\n",
    "    vocab.append('pad')\n",
    "    embedding.append(np.zeros((128))) # 如果是pad就填充0向量,因为句子长度不一样。这里每个单词的向量长度为128\n",
    "    for word in tqdm(words):\n",
    "        try:\n",
    "            vocab.append(word)\n",
    "            embedding.append(model[word])\n",
    "        except:\n",
    "            pass # 去除掉一些为空的字符\n",
    "    return vocab, embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/4669 [00:00<?, ?it/s]<ipython-input-208-ce333f5d95a1>:9: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  embedding.append(model[word])\n",
      "100%|██████████| 4669/4669 [00:00<00:00, 165235.41it/s]\n"
     ]
    }
   ],
   "source": [
    "vocab, embedding = get_embedding(words, model) # 将每个单词和他的向量对应起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [],
   "source": [
    "word_2_index = dict(zip(vocab, list(range(len(vocab)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_2_index[words[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_2_word = dict(zip(list(range(len(vocab))), vocab))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'效果'"
      ]
     },
     "execution_count": 264,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_2_word[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后将数据集中的评论词，根据词表，映射为index表示\n",
    "\n",
    "每个评论，使用index组成的定长数组表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.zeros((128))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [],
   "source": [
    "max_len = 0\n",
    "min_len = 0\n",
    "all_len = []\n",
    "for sentence in comments:\n",
    "    max_len = max(max_len, len(sentence))\n",
    "    min_len = min(float('inf'), len(sentence))\n",
    "    all_len.append(len(sentence))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(234, 2)"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max_len, min_len # 评论中最长的和最短的评论长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.685"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(all_len).mean() # 平均长度为20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAQA0lEQVR4nO3dbYwd51nG8f+F3aZvRE2UTeTaLjZoBXUiSNuVCUSqgoyIIVUdPkRypbYWRDJULrSoEtjlQ/hiyQgoUAlHMk2IK9IYqy+KRWipZVpVSG3STRs1cVyTVR3irU28pYIGkNzavflwxurR5qztPWe9G+/z/0mrmbnnmfM8MxpfOzvnnHGqCklSG35iqQcgSVo8hr4kNcTQl6SGGPqS1BBDX5IasnKpB3ApN9xwQ61bt26phyFJV5Unn3zyu1U1Nrv+ig/9devWMTk5udTDkKSrSpJ/H1T39o4kNcTQl6SGGPqS1BBDX5IaYuhLUkMMfUlqyCVDP8mDSc4keaav9mdJvpXkm0k+m+SNfet2JZlKcjzJnX31tyd5ulv3sSRZ8L2RJF3U5VzpPwRsnlU7DNxSVT8P/BuwCyDJBmArcHO3zd4kK7pt7ge2A+Pdz+zXlCRdYZcM/ar6MvC9WbUvVNW5bvGrwJpufgtwoKrOVtUJYArYmGQVcG1VfaV6D/D/BHD3Au2DJOkyLcQ3cn8b+IdufjW9XwIXTHe1H3bzs+sDJdlO768C3vzmNw89sHU7Hxt621E8v+euJelXki5lpDdyk/wxcA54+EJpQLO6SH2gqtpXVRNVNTE29rJHR0iShjT0lX6SbcA7gU314/9zcRpY29dsDXCqq68ZUJckLaKhrvSTbAb+CHhXVf1f36pDwNYk1yRZT+8N2yeq6jTwUpLbuk/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ZZj6F/Wox6Wqdur6m30bm3tSPKOpR7QK1BL58f9wM8AtwKngb/o6sv2GCR5A/Bp4ENV9f2LNR1QWxbH4FKWY+g3+6iHqjrVTc8An6X35+qLSVYBdNMzSzfCRTXXfjdzflTVi1V1vqp+BPwtP759sSyPQZJX0Qv8h6vqM125+fNgtuUY+k0+6iHJ65P85IV54NeAZ+jt+7au2Tbg0aUZ4aKba78PAVuTXJNkPTAOPLEE47viLoRd5zfpnQ+wDI9BkgAPAMeq6qN9q5o/D2Zbdv9H7hI86uGV4ibgs71zn5XAJ6vq80m+BhxMci/wAnDPEo7xikjyCHAHcEOSaeA+YA8D9ruqjiY5SO//cDgH7Kiq80sy8AU0xzG4I8mt9G5bPA/8DizbY3A78F7g6SRPdbWP0Nh5cDl8DIMkNWQ53t6RJM3B0Jekhhj6ktQQQ1+SGmLoS1JDDH1JaoihL0kN+X9FP4aUbUeMzAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.hist(all_len) # 可以发现大部分评论长度都是在25左右，所以这里设置为40为最大长度\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [],
   "source": [
    "def comments_process(comments, word_2_index, max_len=40):\n",
    "    '''\n",
    "    将每个条评论转换成index组成的向量，同时对长度进行补全，因为评论的单词长短不一，所以要设置成一样的。\n",
    "    '''\n",
    "    comments_idx = np.zeros((len(comments), max_len)) # 初始化矩阵\n",
    "    for sentence_idx, sentence in enumerate(tqdm(comments)):\n",
    "        temp = np.zeros((max_len))\n",
    "        for word_idx in range(max_len):\n",
    "            word = sentence[word_idx] if word_idx < len(sentence) else 'pad'\n",
    "            temp[word_idx] = word_2_index[word]\n",
    "        comments_idx[sentence_idx] = temp\n",
    "    return comments_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 2000/2000 [00:00<00:00, 57392.93it/s]\n"
     ]
    }
   ],
   "source": [
    "comments_idx = comments_process(comments, word_2_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1220.,    5.,  520., ...,   63.,   10.,  164.],\n",
       "        [  69.,  276.,  261., ...,    0.,    0.,    0.],\n",
       "        [   5.,  707.,  152., ...,    0.,    0.,    0.],\n",
       "        ...,\n",
       "        [ 427.,  937.,    4., ...,    0.,    0.,    0.],\n",
       "        [  16.,  567.,  571., ...,    0.,    0.,    0.],\n",
       "        [  70.,   24.,    0., ...,    0.,    0.,    0.]]),\n",
       " (2000, 40))"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments_idx, comments_idx.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1000/1000 [00:00<00:00, 40866.22it/s]\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 76641.89it/s]\n"
     ]
    }
   ],
   "source": [
    "pos_comments_idx = comments_process(pos_comments, word_2_index)\n",
    "neg_comments_idx = comments_process(neg_comments, word_2_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1000, 40), (1000, 40))"
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos_comments_idx.shape, neg_comments_idx.shape # 这里分别获取neg和pos的评论index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_embedding(comments_idx, model, index_2_word, embedding_size=128):\n",
    "    batch, max_len = comments_idx.shape\n",
    "    embedding_vec = np.zeros((batch, max_len, embedding_size))\n",
    "    for batch_idx, sentence in enumerate(tqdm(comments_idx)):\n",
    "        for word_idx, word in enumerate(sentence):\n",
    "            word = index_2_word[word]\n",
    "            embedding_vec[batch_idx, word_idx, :] = model[word] if word != 'pad' else np.zeros((embedding_size))\n",
    "    return embedding_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/1000 [00:00<?, ?it/s]<ipython-input-272-03fec97dde2f>:7: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  embedding_vec[batch_idx, word_idx, :] = model[word] if word != 'pad' else np.zeros((embedding_size))\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 4482.61it/s]\n",
      "100%|██████████| 1000/1000 [00:00<00:00, 7641.40it/s]\n"
     ]
    }
   ],
   "source": [
    "pos_embedding = get_embedding(pos_comments_idx, model, index_2_word)\n",
    "neg_embedding = get_embedding(neg_comments_idx, model, index_2_word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1000, 40, 128), (1000, 40, 128))"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos_embedding.shape, neg_embedding.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据集构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_label = np.asarray([1]*len(pos_embedding))\n",
    "neg_label = np.asarray([0]*len(neg_embedding))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = np.concatenate([pos_embedding, neg_embedding], axis=0)\n",
    "label = np.concatenate([pos_label, neg_label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2000, 40, 128), (2000,))"
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape, label.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(train, label, test_size=.5, shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1000, 40, 128), (1000, 40, 128), (1000,), (1000,))"
      ]
     },
     "execution_count": 310,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.shape, x_test.shape, y_train.shape, y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 建模分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train, x_test = x_train.mean(axis=1), x_test.mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = svm.SVC(C=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=2)"
      ]
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "metadata": {},
   "outputs": [],
   "source": [
    "preds = clf.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.993"
      ]
     },
     "execution_count": 317,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy_score(y_test, preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
